/* eslint-disable @typescript-eslint/no-unused-vars */
import { createStore } from 'vuex';
// import createPersistedState from 'vuex-persistedstate';

import { baseActions, baseMutations } from '@/baseFramework/baseStore';
import {
  xPostLoadListModel,
  xPostLoadListProModel,
  xPostLoadItemModel,
  xPostCreateForm,
  xPostExecCommand, //  payload.params --> 框架 不知道 你要刷新按个模型  ， reset -->  form  or pagingtion
  xPostLoadData,
  xLoadDataForParallelNoWait,
  xLoadDataForParallelWait,
  xLoadDataForOneByOne,
  xGetLoadData,
  xExecCommand,
} from '@/baseFramework/baseLogicStore';
import { VuexContext } from '@/baseFramework/VuexContext';
import { extendVuexAction } from '@/baseFramework/extendVuex';
import { makeBaseFormModel } from '@/constant/baseModel';

// 引入多个模块

const vuexmFileMap: any = import.meta.glob('../store/modules/*.ts', { eager: true });

const modules = {};

for (const key in vuexmFileMap) {
  const mkey = key.replace(/(\.\/modules\/|\.ts)/g, '');
  modules[mkey] = vuexmFileMap[key].default;
}

// 要废弃的方案(可以用)-2022-11-05 To miles_fk
// const modulesFiles: object = import.meta.globEager('./modules/*.ts');
// const modules = Object.keys(modulesFiles).reduce((modules, modulePath) => {
//   const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1').replace('modules/', '');
//   modules[moduleName] = modulesFiles[modulePath].default;
//   return modules;
// }, {});

let store = createStore({
  state: {
    gloading: false,
    paramsTypeList: [], //参数类型
    systemList: [], //系统名称
    domainList: [], //领域
    pageLoading: false, // 路由进入的 loadding
    field: '',
    fullPath: '',
    loginFormModel: makeBaseFormModel({ userName: 'va-publicAdmin', password: 'H@ppy123', appId: 'BPS' }),
    iam: {
      token: '100', // token
      permissionTree: {}, // 权限树
      appId: 'BPS',
    },
  },
  getters: {
    isSidebarNavCollapse: (state: any) => state.app.isSidebarNavCollapse,
  },
  // @ts-ignore
  actions: {
    ...baseActions,
    //@ts-ignore
    xPostLoadData,
    //@ts-ignore
    xLoadDataForParallelNoWait,
    //@ts-ignore
    xLoadDataForParallelWait,
    //@ts-ignore
    xLogicLoadDomainAndSystemInformation(context: VuexContext) {
      let rawDomainList: Array<any> = [];
      if (context.state.domainList.length > 0) {
        return context
          .dispatch('xPostLoadData', {
            sourceType: 'select.data',
            modelName: 'systemList',
            params: {
              territory: context.state.domainList[0].value,
            },
          })
          .then((r) => {
            return [{ code: 0, msg: '', data: context.rootState.domainList }, { code: 0, msg: '', data: context.rootState.paramsTypeList }, r];
          });
      } else {
        return context
          .dispatch('xPostLoadData', {
            sourceType: 'select.domain.data',
            modelName: 'domainList',
          })
          .then((r) => {
            rawDomainList = r;
            return context.dispatch('xLoadDataForParallelWait', {
              actions: [
                {
                  sourceType: 'param.type',
                  modelName: 'paramsTypeList',
                  gloading: true,
                },
                {
                  sourceType: 'select.data',
                  modelName: 'systemList',
                  params: {
                    territory: r.data[0].value,
                  },
                },
              ],
            });
          })
          .then((r) => {
            return [rawDomainList, ...r];
          });
      }
    },
    //@ts-ignore
    xLogicSysFormAction(context: VuexContext) {
      context.dispatch('xPostLoadData', {
        sourceType: 'select.data',
        modelName: 'systemList',
        gloading: true,
        params: {
          territory: context.state.mainDictManagement.mainDictListProModel.form.territory,
        },
      });
    },
  },
  mutations: {
    ...baseMutations,
  },
  modules,
  // plugins: [createPersistedState({ storage: window.sessionStorage, key: import.meta.env.VITE_APP_STORE_KEY })],
});

const mnlist = Object.keys(modules);
mnlist.forEach((mn) => {
  store.dispatch(`${mn}/setObject`, {
    _cmid: mn,
  });
});

store = extendVuexAction(store, mnlist);

export default store;
